
 <!DOCTYPE HTML>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  
    <title>logback详解 | wustrive&#39;s blog</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=3, minimum-scale=1">
    
    <meta name="author" content="wustrive">
    
    <meta name="description" content="logback详解

说明：下面内容从网上搜集而来


与log4j对比
更快的执行速度： 基于我们先前在log4j上的工作，logback 重写了内部的实现，在某些特定的场景上面，甚至可以比之前的速度快上10倍。在保证logback的组件更加快速的同时，同时所需的内存更加少。
logback-cl">
    
    
    
    <meta name="twitter:card" content="summary" />
    <meta name="twitter:site" content="wustrive" />
    <meta name="twitter:title" content="logback详解 | wustrive&#39;s blog" />
      
    
    
    <link rel="alternate" href="/atom.xml" title="wustrive&#39;s blog" type="application/atom+xml">
    
    
    <link rel="icon" href="/img/favicon.ico">
    
    
    <link rel="apple-touch-icon" href="/img/logo.ico">
    <link rel="apple-touch-icon-precomposed" href="/img/logo.ico">
    
    <link rel="stylesheet" href="/css/style.css">
    <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?5829c94e2ff91660826b6c0c188f5620";
      var s = document.getElementsByTagName("script")[0]; 
      s.parentNode.insertBefore(hm, s);
    })();
    </script>

</head>

  <body>
    <header>
      <div>
		
			<div id="imglogo">
				<a href="/"><img src="/img/logo.png" alt="wustrive&#39;s blog" title="wustrive&#39;s blog"/></a>
			</div>
			
			<div id="textlogo">
				<h1 class="site-name"><a href="/" title="wustrive&#39;s blog">wustrive&#39;s blog</a></h1>
				<h2 class="blog-motto">好记性不如写博客</h2>
			</div>
			<div class="navbar"><a class="navbutton navmobile" href="#" title="菜单">
			</a></div>
			<nav class="animated">
				<ul>
					
						<li><a href="/">主页</a></li>
					
						<li><a href="/archives">归档</a></li>
					
						<li><a href="/aboutme">关于</a></li>
					
					<li>
					
					<form class="search" action="//google.com/search" method="get" accept-charset="utf-8">
						<label>Search</label>
						<input type="text" id="search" name="q" autocomplete="off" maxlength="20" placeholder="搜索" />
						<input type="hidden" name="q" value="site:wustrive2008.github.io">
					</form>
					
					</li>
				</ul>
			</nav>			
</div>

    </header>
    <div id="container">
      <div id="main" class="post" itemscope itemprop="blogPost">
	<article itemprop="articleBody"> 
		<header class="article-info clearfix">
  <h1 itemprop="name">
    
      <a href="/2016/12/30/java/logback详解/" title="logback详解" itemprop="url">logback详解</a>
  </h1>
  <p class="article-author">By
    
      <a href="https://wustrive2008.github.io" title="wustrive">wustrive</a>
    </p>
  <p class="article-time">
    <time datetime="2016-12-30T13:50:25.000Z" itemprop="datePublished">2016-12-30</time>
    更新日期:<time datetime="2016-12-31T04:00:44.000Z" itemprop="dateModified">2016-12-31</time>
    
  </p>
</header>
	<div class="article-content">
		
<div class="article-gallery">
  <div class="article-gallery-photos">
    
      <a class="article-gallery-img fancybox" href="http://7xifb5.com1.z0.glb.clouddn.com/wustrive-hexojava知识结构.png" rel="gallery_cixcpdcmu0000oyfyqpicyyn4">
        <img src="http://7xifb5.com1.z0.glb.clouddn.com/wustrive-hexojava知识结构.png" itemprop="image">
      </a>
    
  </div>
</div>

		
		<div id="toc" class="toc-article">
			<strong class="toc-title">文章目录</strong>
		<ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#logback详解"><span class="toc-number">1.</span> <span class="toc-text">logback详解</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#与log4j对比"><span class="toc-number">1.1.</span> <span class="toc-text">与log4j对比</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#logback_基础配置"><span class="toc-number">1.2.</span> <span class="toc-text">logback 基础配置</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#根节点包含的属性："><span class="toc-number">1.2.1.</span> <span class="toc-text">根节点包含的属性：</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#根节点<configuration>的子节点"><span class="toc-number">1.2.2.</span> <span class="toc-text">根节点<configuration>的子节点</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#设置loger："><span class="toc-number">1.2.3.</span> <span class="toc-text">设置loger：</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#示例"><span class="toc-number">1.3.</span> <span class="toc-text">示例</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#<appender>配置"><span class="toc-number">1.4.</span> <span class="toc-text"><appender>配置</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#ConsoleAppender"><span class="toc-number">1.4.1.</span> <span class="toc-text">ConsoleAppender</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#FileAppender"><span class="toc-number">1.4.2.</span> <span class="toc-text">FileAppender</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#RollingFileAppender"><span class="toc-number">1.4.3.</span> <span class="toc-text">RollingFileAppender</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#"><span class="toc-number">1.5.</span> <span class="toc-text"></span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#参考"><span class="toc-number">1.6.</span> <span class="toc-text">参考</span></a></li></ol></li></ol>
		</div>
		
		<h3 id="logback详解">logback详解</h3><blockquote>
<blockquote>
<p>说明：下面内容从网上搜集而来</p>
</blockquote>
</blockquote>
<h4 id="与log4j对比">与log4j对比</h4><ul>
<li>更快的执行速度： 基于我们先前在log4j上的工作，logback 重写了内部的实现，在某些特定的场景上面，甚至可以比之前的速度快上10倍。在保证logback的组件更加快速的同时，同时所需的内存更加少。</li>
<li>logback-classic 非常自然的实现了SLF4J：ogback-classic中的longging类自然的实现了SLF4J。当你使用 logback-classic作为底层实现时，涉及到LF4J日记系统的问题你完全不需要考虑。更进一步来说，由于 logback-classic强烈建议使用SLF4J作为客户端日记系统实现，如果需要切换到log4j或者其他，你只需要替换一个jar包即可，不需要去改变那些通过SLF4J API 实现的代码。这可以大大减少更换日记系统的工作量。</li>
<li>自动重新载入配置文件：Logback-classic可以在配置文件被修改后，自动重新载入。这个扫描过程很快，无资源争用，并且可以动态扩展支持在上百个线程之间每秒上百万个调用。它和应用服务器结合良好，并且在JEE环境通用，因为它不会调用创建一个单独的线程来做扫描。</li>
<li>优雅地从I/O错误中恢复：FileAppender和它的子类，包括RollingFileAppender，可以优雅的从I/O错误中恢复。所以，如果一个文件服务器临时宕机，你再也不需要重启你的应用，而日志功能就能正常工作。当文件服务器恢复工作，logback相关的appender就会透明地和快速的从上一个错误中恢复。</li>
<li>自动清除旧的日志归档文件：通过设置TimeBasedRollingPolicy 或者 SizeAndTimeBasedFNATP的 maxHistory 属性，你就可以控制日志归档文件的最大数量。如果你的回滚策略是每月回滚的，并且你希望保存一年的日志，那么只需简单的设置maxHistory属性为12。对于12个月之前的归档日志文件将被自动清除。</li>
<li>自动压缩归档日志文件：RollingFileAppender可以在回滚操作中，自动压缩归档日志文件。压缩通常是异步执行的，所以即使是很大的日志文件，你的应用都不会因此而被阻塞。</li>
<li>配置文件中的条件处理：开发者通常需要在不同的目标环境中变换logback的配置文件，例如开发环境，测试环境和生产环境。这些配置文件大体是一样的，除了某部分会有不同。为了避免重复，logback支持配置文件中的条件处理，只需使用<code>&lt;if&gt;</code>,<code>&lt;then&gt;</code>和<code>&lt;else&gt;</code>，那么同一个配置文件就可以在不同的环境中使用了。</li>
<li>过滤： Logback拥有远比log4j更丰富的过滤能力。例如，让我们假设，有一个相当重要的商业应用部署在生产环境。考虑到大量的交易数据需要处理，记录级别被设置为WARN，那么只有警告和错误信息才会被记录。现在，想象一下，你在开发环境遇到了一个臭虫，但是在测试平台中却很难发现，因为一些环境之间(生产环境/测试环境)的未知差异。使用log4j，你只能选择在生产系统中降低记录的级别到DEBUG，来尝试发现问题。但是很不幸，这会生成大量的日志记录，让分析变得困难。更重要的是，多余的日志记录会影响到生产环境的性能。使用logback，你可以选择保留只所有用户的WARN级别的日志，而除了某个用户，例如Alice，而她就是问题的相关用户。当Alice登录系统，她就会以DEBUG级别被记录，而其他用户仍然是以WARN级别来记录日志。这个功能，可以通过在配置文件的XML中添加4行。请在相关章节中查找MDCFilter</li>
</ul>
<h4 id="logback_基础配置">logback 基础配置</h4><h5 id="根节点包含的属性：">根节点<configuration>包含的属性：</configuration></h5><ul>
<li>scan: 当此属性设置为true时，配置文件如果发生改变，将会被重新加载，默认值为true。</li>
<li>scanPeriod: 设置监测配置文件是否有修改的时间间隔，如果没有给出时间单位，默认单位是毫秒。当scan为true时，此属性生效。默认的时间间隔为1分钟。</li>
<li>debug: 当此属性设置为true时，将打印出logback内部日志信息，实时查看logback运行状态。默认值为false。</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">configuration</span> <span class="attr">scan</span>=<span class="string">"true"</span> <span class="attr">scanPeriod</span>=<span class="string">"60 seconds"</span> <span class="attr">debug</span>=<span class="string">"false"</span>&gt;</span>  </div><div class="line">      <span class="comment">&lt;!-- 其他配置省略--&gt;</span>  </div><div class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div></pre></td></tr></table></figure>
<h5 id="根节点&lt;configuration&gt;的子节点">根节点<code>&lt;configuration&gt;</code>的子节点</h5><p>设置上下文名称：<code>&lt;contextName&gt;</code><br>每个logger都关联到logger上下文，默认上下文名称为“default”。但可以使用<code>&lt;contextName&gt;</code>设置成其他名字，用于区分不同应用程序的记录。一旦设置，不能修改。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">configuration</span> <span class="attr">scan</span>=<span class="string">"true"</span> <span class="attr">scanPeriod</span>=<span class="string">"60 seconds"</span> <span class="attr">debug</span>=<span class="string">"false"</span>&gt;</span>  </div><div class="line">      <span class="tag">&lt;<span class="name">contextName</span>&gt;</span>myAppName<span class="tag">&lt;/<span class="name">contextName</span>&gt;</span>  </div><div class="line">      <span class="comment">&lt;!-- 其他配置省略--&gt;</span>  </div><div class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div></pre></td></tr></table></figure>
<p>设置变量： <code>&lt;property&gt;</code><br>有两个属性，name和value；其中name的值是变量的名称，value的值时变量定义的值。通过<code>&lt;property&gt;</code>定义的值会被插入到logger上下文中。定义变量后，可以使“${}”来使用变量。<br>例如使用<code>&lt;property&gt;</code>定义上下文名称，然后在<code>&lt;contentName&gt;</code>设置logger上下文时使用。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">configuration</span> <span class="attr">scan</span>=<span class="string">"true"</span> <span class="attr">scanPeriod</span>=<span class="string">"60 seconds"</span> <span class="attr">debug</span>=<span class="string">"false"</span>&gt;</span>  </div><div class="line">      <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"APP_Name"</span> <span class="attr">value</span>=<span class="string">"myAppName"</span> /&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">contextName</span>&gt;</span>$&#123;APP_Name&#125;<span class="tag">&lt;/<span class="name">contextName</span>&gt;</span>  </div><div class="line">      <span class="comment">&lt;!-- 其他配置省略--&gt;</span>  </div><div class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div></pre></td></tr></table></figure>
<p>获取时间戳字符串：<code>&lt;timestamp&gt;</code></p>
<p>两个属性 key:标识此<code>&lt;timestamp&gt;</code> 的名字；datePattern：设置将当前时间（解析配置文件的时间）转换为字符串的模式，遵循Java.txt.SimpleDateFormat的格式。<br>例如将解析配置文件的时间作为上下文名称：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">configuration</span> <span class="attr">scan</span>=<span class="string">"true"</span> <span class="attr">scanPeriod</span>=<span class="string">"60 seconds"</span> <span class="attr">debug</span>=<span class="string">"false"</span>&gt;</span>  </div><div class="line">      <span class="tag">&lt;<span class="name">timestamp</span> <span class="attr">key</span>=<span class="string">"bySecond"</span> <span class="attr">datePattern</span>=<span class="string">"yyyyMMdd'T'HHmmss"</span>/&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">contextName</span>&gt;</span>$&#123;bySecond&#125;<span class="tag">&lt;/<span class="name">contextName</span>&gt;</span>  </div><div class="line">      <span class="comment">&lt;!-- 其他配置省略--&gt;</span>  </div><div class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div></pre></td></tr></table></figure></p>
<h5 id="设置loger：">设置loger：</h5><p><code>&lt;loger&gt;</code><br>用来设置某一个包或者具体的某一个类的日志打印级别、以及指定<code>&lt;appender&gt;</code>。<code>&lt;loger&gt;</code>仅有一个name属性，一个可选的level和一个可选的addtivity属性。</p>
<ul>
<li>name: 用来指定受此loger约束的某一个包或者具体的某一个类。</li>
<li>level: 用来设置打印级别，大小写无关：TRACE, DEBUG, INFO, WARN, ERROR, ALL 和OFF，还有一个特俗值INHERITED或者同义词NULL，代表强制执行上级的级别。如果未设置此属性，那么当前loger将会继承上级的级别。</li>
<li>addtivity: 是否向上级loger传递打印信息。默认是true。<br><code>&lt;loger&gt;</code>可以包含零个或多个<code>&lt;appender-ref&gt;</code>元素，标识这个appender将会添加到这个loger。</li>
</ul>
<p><code>&lt;root&gt;</code><br>也是<code>&lt;loger&gt;</code>元素，但是它是根loger。只有一个level属性，应为已经被命名为”root”.</p>
<ul>
<li>level: 用来设置打印级别，大小写无关：TRACE, DEBUG, INFO, WARN, ERROR, ALL 和 OFF，不能设置为INHERITED或者同义词NULL。<br>默认是DEBUG。<br><code>&lt;root&gt;</code>可以包含零个或多个<code>&lt;appender-ref&gt;</code>元素，标识这个appender将会添加到这个loger。</li>
</ul>
<h4 id="示例">示例</h4><p>LogbackDemo.java<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">package</span> logback;  </div><div class="line">  </div><div class="line"><span class="keyword">import</span> org.slf4j.Logger;  </div><div class="line"><span class="keyword">import</span> org.slf4j.LoggerFactory;  </div><div class="line">  </div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LogbackDemo</span> </span>&#123;  </div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> Logger log = LoggerFactory.getLogger(LogbackDemo.class);  </div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;  </div><div class="line">        log.trace(<span class="string">"======trace"</span>);  </div><div class="line">        log.debug(<span class="string">"======debug"</span>);  </div><div class="line">        log.info(<span class="string">"======info"</span>);  </div><div class="line">        log.warn(<span class="string">"======warn"</span>);  </div><div class="line">        log.error(<span class="string">"======error"</span>);  </div><div class="line">    &#125;  </div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>logback.xml配置文件<br><strong>第1种：只配置root</strong><br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span>   </div><div class="line">   </div><div class="line">  <span class="tag">&lt;<span class="name">appender</span> <span class="attr">name</span>=<span class="string">"STDOUT"</span> <span class="attr">class</span>=<span class="string">"ch.qos.logback.core.ConsoleAppender"</span>&gt;</span>   </div><div class="line">    <span class="comment">&lt;!-- encoder 默认配置为PatternLayoutEncoder --&gt;</span>   </div><div class="line">    <span class="tag">&lt;<span class="name">encoder</span>&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">pattern</span>&gt;</span>%d&#123;HH:mm:ss.SSS&#125; [%thread] %-5level %logger&#123;36&#125; - %msg%n<span class="tag">&lt;/<span class="name">pattern</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;/<span class="name">encoder</span>&gt;</span>   </div><div class="line">  <span class="tag">&lt;/<span class="name">appender</span>&gt;</span>   </div><div class="line">   </div><div class="line">  <span class="tag">&lt;<span class="name">root</span> <span class="attr">level</span>=<span class="string">"INFO"</span>&gt;</span>             </div><div class="line">    <span class="tag">&lt;<span class="name">appender-ref</span> <span class="attr">ref</span>=<span class="string">"STDOUT"</span> /&gt;</span>   </div><div class="line">  <span class="tag">&lt;/<span class="name">root</span>&gt;</span>     </div><div class="line">     </div><div class="line"> <span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p>其中appender的配置表示打印到控制台(稍后详细讲解appender )；<br><code>&lt;root level=&quot;INFO&quot;&gt;</code>将root的打印级别设置为“INFO”，指定了名字为“STDOUT”的appender。</p>
<p>当执行logback.LogbackDemo类的main方法时，root将级别为“INFO”及大于“INFO”的日志信息交给已经配置好的名为“STDOUT”的appender处理，“STDOUT”appender将信息打印到控制台；</p>
<p>打印结果如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">13:30:38.484 [main] INFO  logback.LogbackDemo - ======info  </div><div class="line">13:30:38.500 [main] WARN  logback.LogbackDemo - ======warn  </div><div class="line">13:30:38.500 [main] ERROR logback.LogbackDemo - ======error</div></pre></td></tr></table></figure>
<p><strong>第2种：带有loger的配置，不指定级别，不指定appender</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span>   </div><div class="line">   </div><div class="line">  <span class="tag">&lt;<span class="name">appender</span> <span class="attr">name</span>=<span class="string">"STDOUT"</span> <span class="attr">class</span>=<span class="string">"ch.qos.logback.core.ConsoleAppender"</span>&gt;</span>   </div><div class="line">    <span class="comment">&lt;!-- encoder 默认配置为PatternLayoutEncoder --&gt;</span>   </div><div class="line">    <span class="tag">&lt;<span class="name">encoder</span>&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">pattern</span>&gt;</span>%d&#123;HH:mm:ss.SSS&#125; [%thread] %-5level %logger&#123;36&#125; - %msg%n<span class="tag">&lt;/<span class="name">pattern</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;/<span class="name">encoder</span>&gt;</span>   </div><div class="line">  <span class="tag">&lt;/<span class="name">appender</span>&gt;</span>   </div><div class="line">   </div><div class="line">  <span class="comment">&lt;!-- logback为java中的包 --&gt;</span>   </div><div class="line">  <span class="tag">&lt;<span class="name">logger</span> <span class="attr">name</span>=<span class="string">"logback"</span>/&gt;</span>   </div><div class="line">   </div><div class="line">  <span class="tag">&lt;<span class="name">root</span> <span class="attr">level</span>=<span class="string">"DEBUG"</span>&gt;</span>             </div><div class="line">    <span class="tag">&lt;<span class="name">appender-ref</span> <span class="attr">ref</span>=<span class="string">"STDOUT"</span> /&gt;</span>   </div><div class="line">  <span class="tag">&lt;/<span class="name">root</span>&gt;</span>     </div><div class="line">     </div><div class="line"> <span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div></pre></td></tr></table></figure>
<p>其中appender的配置表示打印到控制台(稍后详细讲解appender )；<br><code>&lt;logger name=&quot;logback&quot; /&gt;</code>将控制logback包下的所有类的日志的打印，但是并没用设置打印级别，所以继承他的上级<code>&lt;root&gt;</code>的日志级别“DEBUG”；<br>没有设置addtivity，默认为true，将此loger的打印信息向上级传递；<br>没有设置appender，此loger本身不打印任何信息。<br><code>&lt;root level=&quot;DEBUG&quot;&gt;</code>将root的打印级别设置为“DEBUG”，指定了名字为“STDOUT”的appender。</p>
<p>当执行logback.LogbackDemo类的main方法时，因为LogbackDemo 在包logback中，所以首先执行<code>&lt;logger name=&quot;logback&quot; /&gt;</code>，将级别为“DEBUG”及大于“DEBUG”的日志信息传递给root，本身并不打印；<br>root接到下级传递的信息，交给已经配置好的名为“STDOUT”的appender处理，“STDOUT”appender将信息打印到控制台；</p>
<p>打印结果如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">13:19:15.406 [main] DEBUG logback.LogbackDemo - ======debug  </div><div class="line">13:19:15.406 [main] INFO  logback.LogbackDemo - ======info  </div><div class="line">13:19:15.406 [main] WARN  logback.LogbackDemo - ======warn  </div><div class="line">13:19:15.406 [main] ERROR logback.LogbackDemo - ======error</div></pre></td></tr></table></figure>
<p><strong>第3种：带有多个loger的配置，指定级别，指定appender</strong> </p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span>   </div><div class="line">   <span class="tag">&lt;<span class="name">appender</span> <span class="attr">name</span>=<span class="string">"STDOUT"</span> <span class="attr">class</span>=<span class="string">"ch.qos.logback.core.ConsoleAppender"</span>&gt;</span>   </div><div class="line">    <span class="comment">&lt;!-- encoder 默认配置为PatternLayoutEncoder --&gt;</span>   </div><div class="line">    <span class="tag">&lt;<span class="name">encoder</span>&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">pattern</span>&gt;</span>%d&#123;HH:mm:ss.SSS&#125; [%thread] %-5level %logger&#123;36&#125; - %msg%n<span class="tag">&lt;/<span class="name">pattern</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;/<span class="name">encoder</span>&gt;</span>   </div><div class="line">  <span class="tag">&lt;/<span class="name">appender</span>&gt;</span>   </div><div class="line">   </div><div class="line">  <span class="comment">&lt;!-- logback为java中的包 --&gt;</span>   </div><div class="line">  <span class="tag">&lt;<span class="name">logger</span> <span class="attr">name</span>=<span class="string">"logback"</span>/&gt;</span>   </div><div class="line">  <span class="comment">&lt;!--logback.LogbackDemo：类的全路径 --&gt;</span>   </div><div class="line">  <span class="tag">&lt;<span class="name">logger</span> <span class="attr">name</span>=<span class="string">"logback.LogbackDemo"</span> <span class="attr">level</span>=<span class="string">"INFO"</span> <span class="attr">additivity</span>=<span class="string">"false"</span>&gt;</span>  </div><div class="line">    <span class="tag">&lt;<span class="name">appender-ref</span> <span class="attr">ref</span>=<span class="string">"STDOUT"</span>/&gt;</span>  </div><div class="line">  <span class="tag">&lt;/<span class="name">logger</span>&gt;</span>   </div><div class="line">    </div><div class="line">  <span class="tag">&lt;<span class="name">root</span> <span class="attr">level</span>=<span class="string">"ERROR"</span>&gt;</span>             </div><div class="line">    <span class="tag">&lt;<span class="name">appender-ref</span> <span class="attr">ref</span>=<span class="string">"STDOUT"</span> /&gt;</span>   </div><div class="line">  <span class="tag">&lt;/<span class="name">root</span>&gt;</span>     </div><div class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div></pre></td></tr></table></figure>
<p>其中appender的配置表示打印到控制台(稍后详细讲解appender )；</p>
<p><code>&lt;logger name=&quot;logback&quot; /&gt;</code>将控制logback包下的所有类的日志的打印，但是并没用设置打印级别，所以继承他的上级<code>&lt;root&gt;</code>的日志级别“DEBUG”；<br>没有设置addtivity，默认为true，将此loger的打印信息向上级传递；<br>没有设置appender，此loger本身不打印任何信息。</p>
<p><code>&lt;logger name=&quot;logback.LogbackDemo&quot; level=&quot;INFO&quot; additivity=&quot;false&quot;&gt;</code>控制logback.LogbackDemo类的日志打印，打印级别为“INFO”；<br>additivity属性为false，表示此loger的打印信息不再向上级传递，<br>指定了名字为“STDOUT”的appender。</p>
<p><code>&lt;root level=&quot;DEBUG&quot;&gt;</code>将root的打印级别设置为“ERROR”，指定了名字为“STDOUT”的appender。<br>当执行logback.LogbackDemo类的main方法时，先执行<code>&lt;logger name=&quot;logback.LogbackDemo&quot; level=&quot;INFO&quot; additivity=&quot;false&quot;&gt;</code>，将级别为“INFO”及大于“INFO”的日志信息交给此loger指定的名为“STDOUT”的appender处理，在控制台中打出日志，不再向次loger的上级 <code>&lt;logger name=&quot;logback&quot;/&gt;</code> 传递打印信息；<br><code>&lt;logger name=&quot;logback&quot;/&gt;</code>未接到任何打印信息，当然也不会给它的上级root传递任何打印信息；</p>
<p>打印结果如下：  </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">14:05:35.937 [main] INFO  logback.LogbackDemo - ======info  </div><div class="line">14:05:35.937 [main] WARN  logback.LogbackDemo - ======warn  </div><div class="line">14:05:35.937 [main] ERROR logback.LogbackDemo - ======error</div></pre></td></tr></table></figure>
<p>如果将<code>&lt;logger name=&quot;logback.LogbackDemo&quot; level=&quot;INFO&quot; additivity=&quot;false&quot;&gt;</code>修改为 <code>&lt;logger name=&quot;logback.LogbackDemo&quot; level=&quot;INFO&quot; additivity=&quot;true&quot;&gt;</code>那打印结果将是什么呢？<br>没错，日志打印了两次，想必大家都知道原因了，因为打印信息向上级传递，logger本身打印一次，root接到后又打印一次<br>打印结果如下：  </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">14:09:01.531 [main] INFO  logback.LogbackDemo - ======info  </div><div class="line">14:09:01.531 [main] INFO  logback.LogbackDemo - ======info  </div><div class="line">14:09:01.531 [main] WARN  logback.LogbackDemo - ======warn  </div><div class="line">14:09:01.531 [main] WARN  logback.LogbackDemo - ======warn  </div><div class="line">14:09:01.531 [main] ERROR logback.LogbackDemo - ======error  </div><div class="line">14:09:01.531 [main] ERROR logback.LogbackDemo - ======error</div></pre></td></tr></table></figure>
<h4 id="&lt;appender&gt;配置"><code>&lt;appender&gt;</code>配置</h4><p><code>&lt;appender&gt;</code>是<code>&lt;configuration&gt;</code>的子节点，是负责写日志的组件。<br><code>&lt;appender&gt;</code>有两个必要属性name和class。name指定appender名称，class指定appender的全限定名。</p>
<h5 id="ConsoleAppender">ConsoleAppender</h5><p>把日志添加到控制台，有以下子节点：<br><code>&lt;encoder&gt;</code>：对日志进行格式化。（具体参数稍后讲解 ）<br><code>&lt;target&gt;</code>：字符串 System.out 或者 System.err ，默认 System.out ；<br>例如：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span>  </div><div class="line">  </div><div class="line">  <span class="tag">&lt;<span class="name">appender</span> <span class="attr">name</span>=<span class="string">"STDOUT"</span> <span class="attr">class</span>=<span class="string">"ch.qos.logback.core.ConsoleAppender"</span>&gt;</span>  </div><div class="line">    <span class="tag">&lt;<span class="name">encoder</span>&gt;</span>  </div><div class="line">      <span class="tag">&lt;<span class="name">pattern</span>&gt;</span>%-4relative [%thread] %-5level %logger&#123;35&#125; - %msg %n<span class="tag">&lt;/<span class="name">pattern</span>&gt;</span>  </div><div class="line">    <span class="tag">&lt;/<span class="name">encoder</span>&gt;</span>  </div><div class="line">  <span class="tag">&lt;/<span class="name">appender</span>&gt;</span>  </div><div class="line">  </div><div class="line">  <span class="tag">&lt;<span class="name">root</span> <span class="attr">level</span>=<span class="string">"DEBUG"</span>&gt;</span>  </div><div class="line">    <span class="tag">&lt;<span class="name">appender-ref</span> <span class="attr">ref</span>=<span class="string">"STDOUT"</span> /&gt;</span>  </div><div class="line">  <span class="tag">&lt;/<span class="name">root</span>&gt;</span>  </div><div class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div></pre></td></tr></table></figure>
<h5 id="FileAppender">FileAppender</h5><p>把日志添加到文件，有以下子节点：<br><code>&lt;file&gt;</code>：被写入的文件名，可以是相对目录，也可以是绝对目录，如果上级目录不存在会自动创建，没有默认值。<br><code>&lt;append&gt;</code>：如果是 true，日志被追加到文件结尾，如果是 false，清空现存文件，默认是true。<br><code>&lt;encoder&gt;</code>：对记录事件进行格式化。（具体参数稍后讲解 ）<br><code>&lt;prudent&gt;</code>：如果是 true，日志会被安全的写入文件，即使其他的FileAppender也在向此文件做写入操作，效率低，默认是 false。<br>例如：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span>  </div><div class="line">  </div><div class="line">  <span class="tag">&lt;<span class="name">appender</span> <span class="attr">name</span>=<span class="string">"FILE"</span> <span class="attr">class</span>=<span class="string">"ch.qos.logback.core.FileAppender"</span>&gt;</span>  </div><div class="line">    <span class="tag">&lt;<span class="name">file</span>&gt;</span>testFile.log<span class="tag">&lt;/<span class="name">file</span>&gt;</span>  </div><div class="line">    <span class="tag">&lt;<span class="name">append</span>&gt;</span>true<span class="tag">&lt;/<span class="name">append</span>&gt;</span>  </div><div class="line">    <span class="tag">&lt;<span class="name">encoder</span>&gt;</span>  </div><div class="line">      <span class="tag">&lt;<span class="name">pattern</span>&gt;</span>%-4relative [%thread] %-5level %logger&#123;35&#125; - %msg%n<span class="tag">&lt;/<span class="name">pattern</span>&gt;</span>  </div><div class="line">    <span class="tag">&lt;/<span class="name">encoder</span>&gt;</span>  </div><div class="line">  <span class="tag">&lt;/<span class="name">appender</span>&gt;</span>  </div><div class="line">          </div><div class="line">  <span class="tag">&lt;<span class="name">root</span> <span class="attr">level</span>=<span class="string">"DEBUG"</span>&gt;</span>  </div><div class="line">    <span class="tag">&lt;<span class="name">appender-ref</span> <span class="attr">ref</span>=<span class="string">"FILE"</span> /&gt;</span>  </div><div class="line">  <span class="tag">&lt;/<span class="name">root</span>&gt;</span>  </div><div class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div></pre></td></tr></table></figure>
<h5 id="RollingFileAppender">RollingFileAppender</h5><p>滚动记录文件，先将日志记录到指定文件，当符合某个条件时，将日志记录到其他文件。有以下子节点：<br><code>&lt;file&gt;</code>：被写入的文件名，可以是相对目录，也可以是绝对目录，如果上级目录不存在会自动创建，没有默认值。<br><code>&lt;append&gt;</code>：如果是 true，日志被追加到文件结尾，如果是 false，清空现存文件，默认是true。<br><code>&lt;encoder&gt;</code>：对记录事件进行格式化。（具体参数稍后讲解 ）<br><code>&lt;rollingPolicy&gt;</code>:当发生滚动时，决定 RollingFileAppender 的行为，涉及文件移动和重命名。<br><code>&lt;triggeringPolicy &gt;</code>: 告知 RollingFileAppender 合适激活滚动。<br><code>&lt;prudent&gt;</code>：当为true时，不支持FixedWindowRollingPolicy。支持TimeBasedRollingPolicy，但是有两个限制，1不支持也不允许文件压缩，2不能设置file属性，必须留空。</p>
<p>rollingPolicy:</p>
<p><strong>TimeBasedRollingPolicy：</strong> 最常用的滚动策略，它根据时间来制定滚动策略，既负责滚动也负责出发滚动。有以下子节点：<br><code>&lt;fileNamePattern&gt;</code>:<br>必要节点，包含文件名及“%d”转换符， “%d”可以包含一个Java.text.SimpleDateFormat指定的时间格式，如：%d{yyyy-MM}。如果直接使用 %d，默认格式是 yyyy-MM-dd。RollingFileAppender 的file字节点可有可无，通过设置file，可以为活动文件和归档文件指定不同位置，当前日志总是记录到file指定的文件（活动文件），活动文件的名字不会改变；如果没设置file，活动文件的名字会根据fileNamePattern 的值，每隔一段时间改变一次。“/”或者“\”会被当做目录分隔符。</p>
<p><code>&lt;maxHistory&gt;</code>:<br>可选节点，控制保留的归档文件的最大数量，超出数量就删除旧文件。假设设置每个月滚动，且<code>&lt;maxHistory&gt;</code>是6，则只保存最近6个月的文件，删除之前的旧文件。注意，删除旧文件是，那些为了归档而创建的目录也会被删除。</p>
<p><strong>FixedWindowRollingPolicy：</strong><br>根据固定窗口算法重命名文件的滚动策略。有以下子节点：<br><code>&lt;minIndex&gt;</code>:窗口索引最小值<br><code>&lt;maxIndex&gt;</code>:窗口索引最大值，当用户指定的窗口过大时，会自动将窗口设置为12。<br><code>&lt;fileNamePattern &gt;</code>:<br>必须包含“%i”例如，假设最小值和最大值分别为1和2，命名模式为 mylog%i.log,会产生归档文件mylog1.log和mylog2.log。还可以指定文件压缩选项，例如，mylog%i.log.gz 或者 没有log%i.log.zip</p>
<p>triggeringPolicy:<br><strong>SizeBasedTriggeringPolicy：</strong>查看当前活动文件的大小，如果超过指定大小会告知RollingFileAppender 触发当前活动文件滚动。只有一个节点:<br><code>&lt;maxFileSize&gt;</code>:这是活动文件的大小，默认值是10MB。</p>
<p>例如：每天生成一个日志文件，保存30天的日志文件。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span>   </div><div class="line">  <span class="tag">&lt;<span class="name">appender</span> <span class="attr">name</span>=<span class="string">"FILE"</span> <span class="attr">class</span>=<span class="string">"ch.qos.logback.core.rolling.RollingFileAppender"</span>&gt;</span>   </div><div class="line">      </div><div class="line">    <span class="tag">&lt;<span class="name">rollingPolicy</span> <span class="attr">class</span>=<span class="string">"ch.qos.logback.core.rolling.TimeBasedRollingPolicy"</span>&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">fileNamePattern</span>&gt;</span>logFile.%d&#123;yyyy-MM-dd&#125;.log<span class="tag">&lt;/<span class="name">fileNamePattern</span>&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">maxHistory</span>&gt;</span>30<span class="tag">&lt;/<span class="name">maxHistory</span>&gt;</span>    </div><div class="line">    <span class="tag">&lt;/<span class="name">rollingPolicy</span>&gt;</span>   </div><div class="line">   </div><div class="line">    <span class="tag">&lt;<span class="name">encoder</span>&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">pattern</span>&gt;</span>%-4relative [%thread] %-5level %logger&#123;35&#125; - %msg%n<span class="tag">&lt;/<span class="name">pattern</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;/<span class="name">encoder</span>&gt;</span>   </div><div class="line">  <span class="tag">&lt;/<span class="name">appender</span>&gt;</span>    </div><div class="line">   </div><div class="line">  <span class="tag">&lt;<span class="name">root</span> <span class="attr">level</span>=<span class="string">"DEBUG"</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;<span class="name">appender-ref</span> <span class="attr">ref</span>=<span class="string">"FILE"</span> /&gt;</span>   </div><div class="line">  <span class="tag">&lt;/<span class="name">root</span>&gt;</span>   </div><div class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div></pre></td></tr></table></figure>
<p>例如：按照固定窗口模式生成日志文件，当文件大于20MB时，生成新的日志文件。窗口大小是1到3，当保存了3个归档文件后，将覆盖最早的日志。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span>   </div><div class="line">  <span class="tag">&lt;<span class="name">appender</span> <span class="attr">name</span>=<span class="string">"FILE"</span> <span class="attr">class</span>=<span class="string">"ch.qos.logback.core.rolling.RollingFileAppender"</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;<span class="name">file</span>&gt;</span>test.log<span class="tag">&lt;/<span class="name">file</span>&gt;</span>   </div><div class="line">   </div><div class="line">    <span class="tag">&lt;<span class="name">rollingPolicy</span> <span class="attr">class</span>=<span class="string">"ch.qos.logback.core.rolling.FixedWindowRollingPolicy"</span>&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">fileNamePattern</span>&gt;</span>tests.%i.log.zip<span class="tag">&lt;/<span class="name">fileNamePattern</span>&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">minIndex</span>&gt;</span>1<span class="tag">&lt;/<span class="name">minIndex</span>&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">maxIndex</span>&gt;</span>3<span class="tag">&lt;/<span class="name">maxIndex</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;/<span class="name">rollingPolicy</span>&gt;</span>   </div><div class="line">   </div><div class="line">    <span class="tag">&lt;<span class="name">triggeringPolicy</span> <span class="attr">class</span>=<span class="string">"ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"</span>&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">maxFileSize</span>&gt;</span>5MB<span class="tag">&lt;/<span class="name">maxFileSize</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;/<span class="name">triggeringPolicy</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;<span class="name">encoder</span>&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">pattern</span>&gt;</span>%-4relative [%thread] %-5level %logger&#123;35&#125; - %msg%n<span class="tag">&lt;/<span class="name">pattern</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;/<span class="name">encoder</span>&gt;</span>   </div><div class="line">  <span class="tag">&lt;/<span class="name">appender</span>&gt;</span>   </div><div class="line">           </div><div class="line">  <span class="tag">&lt;<span class="name">root</span> <span class="attr">level</span>=<span class="string">"DEBUG"</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;<span class="name">appender-ref</span> <span class="attr">ref</span>=<span class="string">"FILE"</span> /&gt;</span>   </div><div class="line">  <span class="tag">&lt;/<span class="name">root</span>&gt;</span>   </div><div class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div></pre></td></tr></table></figure>
<p>另外还有SocketAppender、SMTPAppender、DBAppender、SyslogAppender、SiftingAppender，并不常用，这些就不在这里讲解了，大家可以参考官方文档。当然大家可以编写自己的Appender。</p>
<p><code>&lt;encoder&gt;：</code><br>负责两件事，一是把日志信息转换成字节数组，二是把字节数组写入到输出流。<br>目前PatternLayoutEncoder 是唯一有用的且默认的encoder ，有一个<code>&lt;pattern&gt;</code>节点，用来设置日志的输入格式。使用“%”加“转换符”方式，如果要输出“%”，则必须用“\”对“\%”进行转义。</p>
<p>例如：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">encoder</span>&gt;</span>   </div><div class="line">   <span class="tag">&lt;<span class="name">pattern</span>&gt;</span>%-4relative [%thread] %-5level %logger&#123;35&#125; - %msg%n<span class="tag">&lt;/<span class="name">pattern</span>&gt;</span>   </div><div class="line"><span class="tag">&lt;/<span class="name">encoder</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p>格式修饰符，与转换符共同使用：<br>可选的格式修饰符位于“%”和转换符之间。<br>第一个可选修饰符是左对齐 标志，符号是减号“-”；接着是可选的最小宽度 修饰符，用十进制数表示。如果字符小于最小宽度，则左填充或右填充，默认是左填充（即右对齐），填充符为空格。如果字符大于最小宽度，字符永远不会被截断。最大宽度 修饰符，符号是点号”.”后面加十进制数。如果字符大于最大宽度，则从前面截断。点符号“.”后面加减号“-”在加数字，表示从尾部截断。<br>例如：%-4relative 表示，将输出从程序启动到创建日志记录的时间 进行左对齐 且最小宽度为4。</p>
<h4 id=""><filter></filter></h4><p>过滤器，执行一个过滤器会有返回个枚举值，即DENY，NEUTRAL，ACCEPT其中之一。返回DENY，日志将立即被抛弃不再经过其他过滤器；返回NEUTRAL，有序列表里的下个过滤器过接着处理日志；返回ACCEPT，日志会被立即处理，不再经过剩余过滤器。<br>过滤器被添加到<code>&lt;Appender&gt;</code> 中，为<code>&lt;Appender&gt;</code> 添加一个或多个过滤器后，可以用任意条件对日志进行过滤。<code>&lt;Appender&gt;</code> 有多个过滤器时，按照配置顺序执行。</p>
<p>下面是几个常用的过滤器：</p>
<p><strong>LevelFilter：</strong> 级别过滤器，根据日志级别进行过滤。如果日志级别等于配置级别，过滤器会根据onMath 和 onMismatch接收或拒绝日志。有以下子节点：<br><code>&lt;level&gt;</code>:设置过滤级别<br><code>&lt;onMatch&gt;</code>:用于配置符合过滤条件的操作<br><code>&lt;onMismatch&gt;</code>:用于配置不符合过滤条件的操作</p>
<p>例如：将过滤器的日志级别配置为INFO，所有INFO级别的日志交给appender处理，非INFO级别的日志，被过滤掉。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span>   </div><div class="line">  <span class="tag">&lt;<span class="name">appender</span> <span class="attr">name</span>=<span class="string">"CONSOLE"</span> <span class="attr">class</span>=<span class="string">"ch.qos.logback.core.ConsoleAppender"</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;<span class="name">filter</span> <span class="attr">class</span>=<span class="string">"ch.qos.logback.classic.filter.LevelFilter"</span>&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">level</span>&gt;</span>INFO<span class="tag">&lt;/<span class="name">level</span>&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">onMatch</span>&gt;</span>ACCEPT<span class="tag">&lt;/<span class="name">onMatch</span>&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">onMismatch</span>&gt;</span>DENY<span class="tag">&lt;/<span class="name">onMismatch</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;/<span class="name">filter</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;<span class="name">encoder</span>&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">pattern</span>&gt;</span>   </div><div class="line">        %-4relative [%thread] %-5level %logger&#123;30&#125; - %msg%n   </div><div class="line">      <span class="tag">&lt;/<span class="name">pattern</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;/<span class="name">encoder</span>&gt;</span>   </div><div class="line">  <span class="tag">&lt;/<span class="name">appender</span>&gt;</span>   </div><div class="line">  <span class="tag">&lt;<span class="name">root</span> <span class="attr">level</span>=<span class="string">"DEBUG"</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;<span class="name">appender-ref</span> <span class="attr">ref</span>=<span class="string">"CONSOLE"</span> /&gt;</span>   </div><div class="line">  <span class="tag">&lt;/<span class="name">root</span>&gt;</span>   </div><div class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div></pre></td></tr></table></figure>
<p><strong>ThresholdFilter：</strong><br>临界值过滤器，过滤掉低于指定临界值的日志。当日志级别等于或高于临界值时，过滤器返回NEUTRAL；当日志级别低于临界值时，日志会被拒绝。<br>例如：过滤掉所有低于INFO级别的日志。<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span>   </div><div class="line">  <span class="tag">&lt;<span class="name">appender</span> <span class="attr">name</span>=<span class="string">"CONSOLE"</span>   </span></div><div class="line">    <span class="attr">class</span>=<span class="string">"ch.qos.logback.core.ConsoleAppender"</span>&gt;   </div><div class="line">    <span class="comment">&lt;!-- 过滤掉 TRACE 和 DEBUG 级别的日志--&gt;</span>   </div><div class="line">    <span class="tag">&lt;<span class="name">filter</span> <span class="attr">class</span>=<span class="string">"ch.qos.logback.classic.filter.ThresholdFilter"</span>&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">level</span>&gt;</span>INFO<span class="tag">&lt;/<span class="name">level</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;/<span class="name">filter</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;<span class="name">encoder</span>&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">pattern</span>&gt;</span>   </div><div class="line">        %-4relative [%thread] %-5level %logger&#123;30&#125; - %msg%n   </div><div class="line">      <span class="tag">&lt;/<span class="name">pattern</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;/<span class="name">encoder</span>&gt;</span>   </div><div class="line">  <span class="tag">&lt;/<span class="name">appender</span>&gt;</span>   </div><div class="line">  <span class="tag">&lt;<span class="name">root</span> <span class="attr">level</span>=<span class="string">"DEBUG"</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;<span class="name">appender-ref</span> <span class="attr">ref</span>=<span class="string">"CONSOLE"</span> /&gt;</span>   </div><div class="line">  <span class="tag">&lt;/<span class="name">root</span>&gt;</span>   </div><div class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p><strong>EvaluatorFilter：</strong><br>求值过滤器，评估、鉴别日志是否符合指定条件。有一下子节点：<br><code>&lt;evaluator&gt;</code>:<br>鉴别器，常用的鉴别器是JaninoEventEvaluato，也是默认的鉴别器，它以任意的Java布尔值表达式作为求值条件，求值条件在配置文件解释过成功被动态编译，布尔值表达式返回true就表示符合过滤条件。evaluator有个子标签<code>&lt;expression&gt;</code>，用于配置求值条件。<br><code>&lt;onMatch&gt;</code>:用于配置符合过滤条件的操作<br><code>&lt;onMismatch&gt;</code>:用于配置不符合过滤条件的操作</p>
<p>例如：过滤掉所有日志消息中不包含“billing”字符串的日志。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span>   </div><div class="line">   </div><div class="line">  <span class="tag">&lt;<span class="name">appender</span> <span class="attr">name</span>=<span class="string">"STDOUT"</span> <span class="attr">class</span>=<span class="string">"ch.qos.logback.core.ConsoleAppender"</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;<span class="name">filter</span> <span class="attr">class</span>=<span class="string">"ch.qos.logback.core.filter.EvaluatorFilter"</span>&gt;</span>         </div><div class="line">      <span class="tag">&lt;<span class="name">evaluator</span>&gt;</span> <span class="comment">&lt;!-- 默认为 ch.qos.logback.classic.boolex.JaninoEventEvaluator --&gt;</span>   </div><div class="line">        <span class="tag">&lt;<span class="name">expression</span>&gt;</span>return message.contains("billing");<span class="tag">&lt;/<span class="name">expression</span>&gt;</span>   </div><div class="line">      <span class="tag">&lt;/<span class="name">evaluator</span>&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">OnMatch</span>&gt;</span>ACCEPT <span class="tag">&lt;/<span class="name">OnMatch</span>&gt;</span>  </div><div class="line">      <span class="tag">&lt;<span class="name">OnMismatch</span>&gt;</span>DENY<span class="tag">&lt;/<span class="name">OnMismatch</span>&gt;</span>  </div><div class="line">    <span class="tag">&lt;/<span class="name">filter</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;<span class="name">encoder</span>&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">pattern</span>&gt;</span>   </div><div class="line">        %-4relative [%thread] %-5level %logger - %msg%n   </div><div class="line">      <span class="tag">&lt;/<span class="name">pattern</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;/<span class="name">encoder</span>&gt;</span>   </div><div class="line">  <span class="tag">&lt;/<span class="name">appender</span>&gt;</span>   </div><div class="line">   </div><div class="line">  <span class="tag">&lt;<span class="name">root</span> <span class="attr">level</span>=<span class="string">"INFO"</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;<span class="name">appender-ref</span> <span class="attr">ref</span>=<span class="string">"STDOUT"</span> /&gt;</span>   </div><div class="line">  <span class="tag">&lt;/<span class="name">root</span>&gt;</span>   </div><div class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div></pre></td></tr></table></figure>
<p><code>&lt;matcher&gt;</code> ：<br>匹配器，尽管可以使用String类的matches()方法进行模式匹配，但会导致每次调用过滤器时都会创建一个新的Pattern对象，为了消除这种开销，可以预定义一个或多个matcher对象，定以后就可以在求值表达式中重复引用。<code>&lt;matcher&gt;</code>是<code>&lt;evaluator&gt;</code>的子标签。<br><code>&lt;matcher&gt;</code>中包含两个子标签，一个是<code>&lt;name&gt;</code>，用于定义matcher的名字，求值表达式中使用这个名字来引用matcher；另一个是<code>&lt;regex&gt;</code>，用于配置匹配条件。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">configuration</span> <span class="attr">debug</span>=<span class="string">"true"</span>&gt;</span>   </div><div class="line">   </div><div class="line">  <span class="tag">&lt;<span class="name">appender</span> <span class="attr">name</span>=<span class="string">"STDOUT"</span> <span class="attr">class</span>=<span class="string">"ch.qos.logback.core.ConsoleAppender"</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;<span class="name">filter</span> <span class="attr">class</span>=<span class="string">"ch.qos.logback.core.filter.EvaluatorFilter"</span>&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">evaluator</span>&gt;</span>           </div><div class="line">        <span class="tag">&lt;<span class="name">matcher</span>&gt;</span>   </div><div class="line">          <span class="tag">&lt;<span class="name">Name</span>&gt;</span>odd<span class="tag">&lt;/<span class="name">Name</span>&gt;</span>   </div><div class="line">          <span class="comment">&lt;!-- filter out odd numbered statements --&gt;</span>   </div><div class="line">          <span class="tag">&lt;<span class="name">regex</span>&gt;</span>statement [13579]<span class="tag">&lt;/<span class="name">regex</span>&gt;</span>   </div><div class="line">        <span class="tag">&lt;/<span class="name">matcher</span>&gt;</span>   </div><div class="line">           </div><div class="line">        <span class="tag">&lt;<span class="name">expression</span>&gt;</span>odd.matches(formattedMessage)<span class="tag">&lt;/<span class="name">expression</span>&gt;</span>   </div><div class="line">      <span class="tag">&lt;/<span class="name">evaluator</span>&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">OnMismatch</span>&gt;</span>NEUTRAL<span class="tag">&lt;/<span class="name">OnMismatch</span>&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">OnMatch</span>&gt;</span>DENY<span class="tag">&lt;/<span class="name">OnMatch</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;/<span class="name">filter</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;<span class="name">encoder</span>&gt;</span>   </div><div class="line">      <span class="tag">&lt;<span class="name">pattern</span>&gt;</span>%-4relative [%thread] %-5level %logger - %msg%n<span class="tag">&lt;/<span class="name">pattern</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;/<span class="name">encoder</span>&gt;</span>   </div><div class="line">  <span class="tag">&lt;/<span class="name">appender</span>&gt;</span>   </div><div class="line">   </div><div class="line">  <span class="tag">&lt;<span class="name">root</span> <span class="attr">level</span>=<span class="string">"DEBUG"</span>&gt;</span>   </div><div class="line">    <span class="tag">&lt;<span class="name">appender-ref</span> <span class="attr">ref</span>=<span class="string">"STDOUT"</span> /&gt;</span>   </div><div class="line">  <span class="tag">&lt;/<span class="name">root</span>&gt;</span>   </div><div class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div></pre></td></tr></table></figure>
<h4 id="参考">参考</h4><p><a href="https://www.oschina.net/translate/reasons-to-prefer-logbak-over-log4j" target="_blank" rel="external">从Log4j迁移到LogBack的理由</a><br><a href="http://blog.csdn.net/haidage/article/details/6794540" target="_blank" rel="external">logback logback.xml常用配置详解</a></p>
  
	</div>
		<footer class="article-footer clearfix">

  <div class="article-tags">
  
  <span></span> <a href="/tags/java/">java</a><a href="/tags/logback/">logback</a><a href="/tags/slf4j/">slf4j</a><a href="/tags/日志/">日志</a>
  </div>


<div class="article-categories">
  <span></span>
  <a class="article-category-link" href="/categories/java/">java</a>
</div>



<div class="article-share" id="share">

  <div data-url="https://wustrive2008.github.io/2016/12/30/java/logback详解/" data-title="logback详解 | wustrive&#39;s blog" data-tsina="null" class="share clearfix">
  </div>

</div>
</footer>   	       
	</article>
	
<nav class="article-nav clearfix">
 
 <div class="prev" >
 <a href="/2016/12/31/读书笔记/2016年书单总结/" title="2016年书单总结">
  <strong>PREVIOUS:</strong><br/>
  <span>
  2016年书单总结</span>
</a>
</div>


<div class="next">
<a href="/2016/12/23/网络协议/邮件协议SMTP-POP3-IMAP详解/"  title="邮件协议SMTP|POP3|IMAP详解">
 <strong>NEXT:</strong><br/> 
 <span>邮件协议SMTP|POP3|IMAP详解
</span>
</a>
</div>

</nav>

	
<section class="comment">
	<div class="ds-thread"></div>
</section>

</div>  
      <div class="openaside"><a class="navbutton" href="#" title="显示侧边栏"></a></div>

  <div id="toc" class="toc-aside">
  <strong class="toc-title">文章目录</strong>
  <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#logback详解"><span class="toc-number">1.</span> <span class="toc-text">logback详解</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#与log4j对比"><span class="toc-number">1.1.</span> <span class="toc-text">与log4j对比</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#logback_基础配置"><span class="toc-number">1.2.</span> <span class="toc-text">logback 基础配置</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#根节点包含的属性："><span class="toc-number">1.2.1.</span> <span class="toc-text">根节点包含的属性：</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#根节点<configuration>的子节点"><span class="toc-number">1.2.2.</span> <span class="toc-text">根节点<configuration>的子节点</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#设置loger："><span class="toc-number">1.2.3.</span> <span class="toc-text">设置loger：</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#示例"><span class="toc-number">1.3.</span> <span class="toc-text">示例</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#<appender>配置"><span class="toc-number">1.4.</span> <span class="toc-text"><appender>配置</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#ConsoleAppender"><span class="toc-number">1.4.1.</span> <span class="toc-text">ConsoleAppender</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#FileAppender"><span class="toc-number">1.4.2.</span> <span class="toc-text">FileAppender</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#RollingFileAppender"><span class="toc-number">1.4.3.</span> <span class="toc-text">RollingFileAppender</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#"><span class="toc-number">1.5.</span> <span class="toc-text"></span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#参考"><span class="toc-number">1.6.</span> <span class="toc-text">参考</span></a></li></ol></li></ol>
  </div>

<div id="asidepart">
<div class="closeaside"><a class="closebutton" href="#" title="隐藏侧边栏"></a></div>
<aside class="clearfix">

  
<div class="categorieslist">
	<p class="asidetitle">分类</p>
		<ul>
		
			<li><a href="/categories/DB/" title="DB">DB<sup>1</sup></a></li>
		
			<li><a href="/categories/Linux/" title="Linux">Linux<sup>6</sup></a></li>
		
			<li><a href="/categories/UML/" title="UML">UML<sup>1</sup></a></li>
		
			<li><a href="/categories/cORc/" title="cORc++">cORc++<sup>1</sup></a></li>
		
			<li><a href="/categories/hexo/" title="hexo">hexo<sup>1</sup></a></li>
		
			<li><a href="/categories/http协议/" title="http协议">http协议<sup>2</sup></a></li>
		
			<li><a href="/categories/java/" title="java">java<sup>10</sup></a></li>
		
			<li><a href="/categories/js/" title="js">js<sup>1</sup></a></li>
		
			<li><a href="/categories/nginx/" title="nginx">nginx<sup>1</sup></a></li>
		
			<li><a href="/categories/python/" title="python">python<sup>2</sup></a></li>
		
			<li><a href="/categories/分享/" title="分享">分享<sup>1</sup></a></li>
		
			<li><a href="/categories/摘录/" title="摘录">摘录<sup>1</sup></a></li>
		
			<li><a href="/categories/版本控制/" title="版本控制">版本控制<sup>3</sup></a></li>
		
			<li><a href="/categories/算法/" title="算法">算法<sup>2</sup></a></li>
		
			<li><a href="/categories/缓存/" title="缓存">缓存<sup>1</sup></a></li>
		
			<li><a href="/categories/网络协议/" title="网络协议">网络协议<sup>1</sup></a></li>
		
			<li><a href="/categories/网络安全/" title="网络安全">网络安全<sup>1</sup></a></li>
		
			<li><a href="/categories/设计模式/" title="设计模式">设计模式<sup>2</sup></a></li>
		
			<li><a href="/categories/读书笔记/" title="读书笔记">读书笔记<sup>10</sup></a></li>
		
		</ul>
</div>


  
<div class="tagslist">
	<p class="asidetitle">标签</p>
		<ul class="clearfix">
		
			<li><a href="/tags/Arraylist/" title="Arraylist">Arraylist<sup>1</sup></a></li>
		
			<li><a href="/tags/Centos/" title="Centos">Centos<sup>2</sup></a></li>
		
			<li><a href="/tags/DB/" title="DB">DB<sup>1</sup></a></li>
		
			<li><a href="/tags/Docker/" title="Docker">Docker<sup>1</sup></a></li>
		
			<li><a href="/tags/Email/" title="Email">Email<sup>1</sup></a></li>
		
			<li><a href="/tags/HashMap/" title="HashMap">HashMap<sup>1</sup></a></li>
		
			<li><a href="/tags/HotSpot/" title="HotSpot">HotSpot<sup>1</sup></a></li>
		
			<li><a href="/tags/IMAP/" title="IMAP">IMAP<sup>1</sup></a></li>
		
			<li><a href="/tags/Java/" title="Java">Java<sup>1</sup></a></li>
		
			<li><a href="/tags/Linux/" title="Linux">Linux<sup>5</sup></a></li>
		
			<li><a href="/tags/OOM/" title="OOM">OOM<sup>1</sup></a></li>
		
			<li><a href="/tags/POP3/" title="POP3">POP3<sup>1</sup></a></li>
		
			<li><a href="/tags/SMTP/" title="SMTP">SMTP<sup>1</sup></a></li>
		
			<li><a href="/tags/Tomcat/" title="Tomcat">Tomcat<sup>1</sup></a></li>
		
			<li><a href="/tags/UML/" title="UML">UML<sup>1</sup></a></li>
		
			<li><a href="/tags/aes/" title="aes">aes<sup>1</sup></a></li>
		
			<li><a href="/tags/array/" title="array">array<sup>1</sup></a></li>
		
			<li><a href="/tags/arraylist/" title="arraylist">arraylist<sup>1</sup></a></li>
		
			<li><a href="/tags/c/" title="c">c<sup>2</sup></a></li>
		
			<li><a href="/tags/chkconfig/" title="chkconfig">chkconfig<sup>1</sup></a></li>
		
		</ul>
</div>


  <div class="rsspart">
	<a href="/atom.xml" target="_blank" title="rss">RSS 订阅</a>
</div>

</aside>
</div>
    </div>
    <footer><div id="footer" >
	
	<div class="line">
		<span></span>
		<div class="author"></div>
	</div>
	
	
	<section class="info">
		<p> Hello, I&#39;m wustrive. This is my blog on GitHub. <br/>
			I wish you happiness</p>
	</section>
	 
	<div class="social-font clearfix">
		
		<a href="http://weibo.com/12100106" target="_blank" title="weibo"></a>
		
		
		<a href="https://twitter.com/wustrive" target="_blank" title="twitter"></a>
		
		
		<a href="https://github.com/wustrive2008" target="_blank" title="github"></a>
		
		
		
	</div>
		<p class="copyright">Powered by <a href="http://hexo.io" target="_blank" title="hexo">hexo</a> and Theme by <a href="https://github.com/A-limon/pacman" target="_blank" title="Pacman">Pacman</a> © 2018 
		
		<a href="https://wustrive2008.github.io" target="_blank" title="wustrive">wustrive</a>
		
		</p>
</div>
</footer>
    <script src="/js/jquery-2.1.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){ 
  $('.navbar').click(function(){
    $('header nav').toggleClass('shownav');
  });
  var myWidth = 0;
  function getSize(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
  };
  var m = $('#main'),
      a = $('#asidepart'),
      c = $('.closeaside'),
      o = $('.openaside');
  $(window).resize(function(){
    getSize(); 
    if (myWidth >= 1024) {
      $('header nav').removeClass('shownav');
    }else
    {
      m.removeClass('moveMain');
      a.css('display', 'block').removeClass('fadeOut');
      o.css('display', 'none');
      
      $('#toc.toc-aside').css('display', 'none');
        
    }
  });
  c.click(function(){
    a.addClass('fadeOut').css('display', 'none');
    o.css('display', 'block').addClass('fadeIn');
    m.addClass('moveMain');
  });
  o.click(function(){
    o.css('display', 'none').removeClass('beforeFadeIn');
    a.css('display', 'block').removeClass('fadeOut').addClass('fadeIn');      
    m.removeClass('moveMain');
  });
  $(window).scroll(function(){
    o.css("top",Math.max(80,260-$(this).scrollTop()));
  });
});
</script>

<script type="text/javascript">
$(document).ready(function(){ 
  var ai = $('.article-content>iframe'),
      ae = $('.article-content>embed'),
      t  = $('#toc'),
      h  = $('article h2')
      ah = $('article h2'),
      ta = $('#toc.toc-aside'),
      o  = $('.openaside'),
      c  = $('.closeaside');
  if(ai.length>0){
    ai.wrap('<div class="video-container" />');
  };
  if(ae.length>0){
   ae.wrap('<div class="video-container" />');
  };
  if(ah.length==0){
    t.css('display','none');
  }else{
    c.click(function(){
      ta.css('display', 'block').addClass('fadeIn');
    });
    o.click(function(){
      ta.css('display', 'none');
    });
    $(window).scroll(function(){
      ta.css("top",Math.max(140,320-$(this).scrollTop()));
    });
  };
});
</script>


<script type="text/javascript">
$(document).ready(function(){ 
  var $this = $('.share'),
      url = $this.attr('data-url'),
      encodedUrl = encodeURIComponent(url),
      title = $this.attr('data-title'),
      tsina = $this.attr('data-tsina');
  var html = [
  '<a href="#" class="overlay" id="qrcode"></a>',
  '<div class="qrcode clearfix"><span>扫描二维码分享到微信朋友圈</span><a class="qrclose" href="#share"></a><strong>Loading...Please wait</strong><img id="qrcode-pic" data-src="http://s.jiathis.com/qrcode.php?url=' + encodedUrl + '"/></div>',
  '<a href="#textlogo" class="article-back-to-top" title="Top"></a>',
  '<a href="https://www.facebook.com/sharer.php?u=' + encodedUrl + '" class="article-share-facebook" target="_blank" title="Facebook"></a>',
  '<a href="#qrcode" class="article-share-qrcode" title="QRcode"></a>',
  '<a href="https://twitter.com/intent/tweet?url=' + encodedUrl + '" class="article-share-twitter" target="_blank" title="Twitter"></a>',
  '<a href="http://service.weibo.com/share/share.php?title='+title+'&url='+encodedUrl +'&ralateUid='+ tsina +'&searchPic=true&style=number' +'" class="article-share-weibo" target="_blank" title="Weibo"></a>',
  '<span title="Share to"></span>'
  ].join('');
  $this.append(html);
  $('.article-share-qrcode').click(function(){
    var imgSrc = $('#qrcode-pic').attr('data-src');
    $('#qrcode-pic').attr('src', imgSrc);
    $('#qrcode-pic').load(function(){
        $('.qrcode strong').text(' ');
    });
  });
});     
</script>


<script type="text/javascript">
  var duoshuoQuery = {short_name:"wustrive"};
  (function() {
    var ds = document.createElement('script');
    ds.type = 'text/javascript';ds.async = true;
    ds.src = '//static.duoshuo.com/embed.js';
    ds.charset = 'UTF-8';
    (document.getElementsByTagName('head')[0] 
    || document.getElementsByTagName('body')[0]).appendChild(ds);
  })();
</script> 




<script type="text/javascript">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-22190106-2', 'auto');  
ga('send', 'pageview');
</script>


<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-22190106-2', 'auto');
  ga('send', 'pageview');

</script>

  </body>
</html>
